﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.UI;

namespace TesteSharepointNovo.Extension
{
    public abstract class MvcHtmlContainerBase : IDisposable
    {
        protected readonly ViewContext ViewContext;
        private bool _disposed;

        /// <summary>
        ///   Initializes a new instance of the class using the specified view context.
        /// </summary>
        /// <param name="viewContext"> An object that encapsulates the information that is required in order to render a view. </param>
        /// <exception cref="T:System.ArgumentNullException">The
        ///   <paramref name="viewContext" />
        ///   parameter is null.</exception>
        protected MvcHtmlContainerBase(ViewContext viewContext)
        {
            if (viewContext == null)
                throw new ArgumentNullException("viewContext");
            ViewContext = viewContext;
        }

        public abstract HtmlTextWriterTag Tag { get; }

        /// <summary>
        ///   Releases all resources that are used by the current instance of the class.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        ///   Releases unmanaged and, optionally, managed resources used by the current instance of the class.
        /// </summary>
        /// <param name="disposing"> true to release both managed and unmanaged resources; false to release only unmanaged resources. </param>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
                return;
            _disposed = true;

            ViewContext.Writer.Write(string.Concat("</", Tag.ToString().ToLower(), ">"));
        }

        /// <summary>
        ///   Ends the container and disposes of all resources.
        /// </summary>
        public void EndControlGroup()
        {
            Dispose(true);
        }
    }
}
